package com.ad.backstage.service.impl;

import com.ad.backstage.common.utils.BeanUtil;
import com.ad.backstage.mapper.JielongTimeZoneMapper;
import com.ad.backstage.model.ItemCategory;
import com.ad.backstage.model.JielongTimeZone;
import com.ad.backstage.service.JielongTimeZoneService;
import com.ad.payload.dto.JielongTimeZoneDto;
import com.ad.payload.model.IdDto;
import com.ad.payload.model.PageView;
import com.ad.payload.vo.JielongTimeZoneVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class JielongTimeZoneServiceImpl implements JielongTimeZoneService{

    @Autowired
    private JielongTimeZoneMapper jielongTimeZoneMapper;


    @Override
    public PageView<JielongTimeZoneVo> pageJielongTimezone(@Valid IdDto idDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("end_time");
        queryWrapper.eq("merchant_id", idDto.getId());
        IPage<JielongTimeZone> iPage =
                jielongTimeZoneMapper.selectPage(new Page<>(idDto.getPageNo(), idDto.getPageSize()), queryWrapper);

        PageView pageView = new PageView(iPage);

        List<JielongTimeZone> records = iPage.getRecords();
        List<JielongTimeZoneVo> list = new ArrayList<>();

        records.forEach(item -> {
            JielongTimeZoneVo jielongTimeZoneVo = new JielongTimeZoneVo();
            BeanUtil.copyProperties(item, jielongTimeZoneVo);
            list.add(jielongTimeZoneVo);
        });

        pageView.setPageList(list);

        return pageView;
    }

    @Override
    public Long saveOrUpdateJielongTimezone(@Valid JielongTimeZoneDto jielongTimeZoneDto) {

        JielongTimeZone jielongTimeZone = new JielongTimeZone();
        jielongTimeZone.setDeliveryDistance(Double.parseDouble(jielongTimeZoneDto.getDeliveryDistance()));
        jielongTimeZone.setStartTime(LocalTime.parse(jielongTimeZoneDto.getStartTime()));
        jielongTimeZone.setEndTime(LocalTime.parse(jielongTimeZoneDto.getEndTime()));
        jielongTimeZone.setMerchantId(jielongTimeZoneDto.getMerchantId());
        if(jielongTimeZoneDto.getId() == null){
            //新增
            jielongTimeZoneMapper.insert(jielongTimeZone);
        } else {
            //更新
            jielongTimeZone.setId(jielongTimeZoneDto.getId());
            jielongTimeZoneMapper.updateById(jielongTimeZone);

        }

        //更新该用户的配送范围
        QueryWrapper<JielongTimeZone> queryWrapper = new QueryWrapper<JielongTimeZone>();
        queryWrapper.eq("merchant_id",jielongTimeZoneDto.getMerchantId());
        /*List<JielongTimeZone> jielongTimeZones = jielongTimeZoneMapper.selectList(queryWrapper);
        jielongTimeZones.forEach(item -> {
            item.setDeliveryDistance(Double.parseDouble(jielongTimeZoneDto.getDeliveryDistance()));
        });*/
        //批量更新
        JielongTimeZone update = new JielongTimeZone();
        update.setDeliveryDistance(Double.parseDouble(jielongTimeZoneDto.getDeliveryDistance()));
        jielongTimeZoneMapper.update(update,queryWrapper);
        return jielongTimeZone.getId();
    }

    @Override
    public Long delJielongTimezone(@Valid IdDto idDto) {
        jielongTimeZoneMapper.deleteById(idDto.getId());
        return idDto.getId();
    }
}
